package com.guohao.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.guohao.reggie.common.BaseContext;
import com.guohao.reggie.common.R;
import com.guohao.reggie.dto.DishDto;
import com.guohao.reggie.dto.OrdersDto;
import com.guohao.reggie.dto.SetmealDto;
import com.guohao.reggie.entity.OrderDetail;
import com.guohao.reggie.entity.Orders;
import com.guohao.reggie.service.OrderDetailService;
import com.guohao.reggie.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.web.bind.annotation.*;

import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 订单管理
 */
@RestController
@RequestMapping("/order")
@Slf4j
public class OrderController {

    @Autowired
    private OrderService orderService;

    @Autowired
    private OrderDetailService orderDetailService;

    /**
     * 用户下单
     * @param orders
     * @return
     */
    @PostMapping("/submit")
    public R<String> submit(@RequestBody Orders orders){
        log.info("订单数据 : {}" ,orders);

        orderService.submit(orders);

        return R.success("下单成功 等待配送");
    }

    /**
     *
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/userPage")
    public R<Page> page(int page, int pageSize){

        Long currentId = BaseContext.getCurrentId();
        log.info("currentId  = ",currentId);
        Page<Orders> ordersPage = new Page<>(page,pageSize);

        Page<OrdersDto> ordersDtoPage = new Page<>(page,pageSize);

        // 条件构造器
        LambdaQueryWrapper<Orders> ordersLambdaQueryWrapper = new LambdaQueryWrapper<>();
        ordersLambdaQueryWrapper.eq(Orders::getUserId,currentId);
        ordersLambdaQueryWrapper.orderByDesc(Orders::getCheckoutTime);

        orderService.page(ordersPage,ordersLambdaQueryWrapper);
       // 拷贝
        BeanUtils.copyProperties(ordersPage,ordersDtoPage,"records");

        List<Orders> ordersList = ordersPage.getRecords();

        List<OrdersDto> ordersDtoList = ordersList.stream().map((item) ->{
            OrdersDto ordersDto = new OrdersDto();
            //对象拷贝
            BeanUtils.copyProperties(item, ordersDto);

            Long id = item.getId();
            // 根据订单ID 获取详情订单
            LambdaQueryWrapper<OrderDetail> orderDetailLambdaQueryWrapper = new LambdaQueryWrapper<>();
            orderDetailLambdaQueryWrapper.eq(OrderDetail::getOrderId,id);

            List<OrderDetail> list = orderDetailService.list(orderDetailLambdaQueryWrapper);


            ordersDto.setOrderDetails(list);

            return ordersDto;
        }).collect(Collectors.toList());


        ordersDtoPage.setRecords(ordersDtoList);


        return R.success(ordersDtoPage);
    }

    /**
     * 再来一单
     * @return
     */
    @PostMapping("/again")
    public R<String> again(){


        return null;
    }
}
